﻿// <copyright file="PhotobucketLibrary.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-27</date>
// <summary>Photobucket Library for CLR data binding in WPF/Silverlight</summary>

namespace Photobucket
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;

    /// <summary>
    /// Photobucket library for CLR Binding
    /// </summary>
    public class PhotobucketLibrary : INotifyPropertyChanged
    {
        #region Member Data
        /// <summary>
        /// Featured media
        /// </summary>
        private IEnumerable<Media> featuredMedia;

        /// <summary>
        /// Photobucket API
        /// </summary>
        private API api;
        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Event triggered when properties change
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the featured media
        /// </summary>
        [Category("Media")]
        public IEnumerable<Media> FeaturedMedia
        {
            get
            {
                if (Application.Current.RootVisual != null && DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual))
                {
                    Media[] sampleMedia = new Media[]
                    {
                        new Media { Title = "TI", Thumb = new Uri("http://i0006.photobucket.com/albums/0006/pbhomepage/TI/th_ti5.jpg") },
                        new Media { Title = "Funny", Thumb = new Uri("http://i0006.photobucket.com/albums/0006/pbhomepage/Funny/th_f34.jpg") },
                        new Media { Title = "Demi Lovato", Thumb = new Uri("http://i0006.photobucket.com/albums/0006/pbhomepage/Demi%20Lovato/th_demi1-2.jpg") }
                    };

                    return sampleMedia;
                }
                else
                {
                    if (this.featuredMedia == null)
                    {
                        this.api = new API();

                        this.api.GetFeaturedMedia(new EventHandler<Photobucket.SearchCompletedEventArgs>(this.GotFeaturedMedia));
                    }
                }

                return this.featuredMedia;
            }

            set
            {
                this.featuredMedia = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("FeaturedMedia"));
                }
            }
        }
        #endregion

        #region Implementation

        /// <summary>
        /// Featured Media is returned
        /// </summary>
        /// <param name="sender">this library</param>
        /// <param name="args">the search completed event args</param>
        private void GotFeaturedMedia(object sender, SearchCompletedEventArgs args)
        {
            this.FeaturedMedia = args.PrimaryMedia;
        }
        #endregion
    }
}
